const app = getApp()
var tcity = require("../../utils/city.js");
import Config from "../../common/config.js";
Page({
  data: {
    imgUrls: [],
    indicatorDots: true,
    region: ['请选择省', '市', '区'],
    customItem: '',
    // 联动选择器
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    value: [0, 0, 0],
    values: [0, 0, 0],
    condition: false,
    product: [],
    joinImage: "",
    joinProcess: "",
    menu: [],
    phoneImage: "",
    imgbottom: "",
    clear: '',
    imgnum: 2,
    namevalue: "",
    phonevalue: "",
    placevalue: "",
    selSta: 2,

  },
  toSubmit: function(e) {
    console.log(e, "表单数据");
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    var that = this
    if (that.data.namevalue == '') {
      wx.showModal({
        title: '姓名不能为空',
        content: '',
      })
      return;
    } else if (that.data.phonevalue == '') {
      wx.showModal({
        title: '手机号不能为空',
        content: '',
      })
      return;
    } else if (!myreg.test(this.data.phonevalue)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      return;
    }
     else if (that.data.placevalue == '') {
      wx.showModal({
        title: '详细地址不能为空',
        content: '',
      })
      return;
    } else if (that.data.region[0] == '') {
      wx.showModal({
        title: '请选择省份',
        content: '',
      })
      return;
    } else if (that.data.region[1] == '') {
      wx.showModal({
        title: '请选择城市',
        content: '',
      })
      return;
    } else if (that.data.region[2] == '') {
      wx.showModal({
        title: '请选择县区',
        content: '',
      })
      return;
    }
    var sessionId = wx.getStorageSync('sessionId')
    wx.request({
      url: Config.url + '/api/consigneeInfo/add_consigneeInfo',
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'accessToken': sessionId
      },
      data: {
        consigneeName: that.data.namevalue,
        consigneeMobile: that.data.phonevalue,
        consigneeAddress: that.data.placevalue,
        province: that.data.region[0],
        city: that.data.region[1],
        area: that.data.region[2],
        isDefault: that.data.selSta
      },
      success: function(res) {
        console.log(res)
        if (res.data.code == 100) {
          wx.showToast({
            title: '提交成功！',
            content: '',
            icon: 'success',
          })
          wx.getStorage({
            key: 'zf',
            success: function(res) {
              setTimeout(function () {
                wx.redirectTo({
                  url: '../adressList/adressList?zf=' + res.data
                });    
              }, 1000)      
            },
          })
        
          // ---------------
          // wx.navigateTo({
          //   url: '../adressList/adressList',
          // })
          // ----------------
          //返回上一页
          that.setData({
            clear: ''
          })
        } else {
          wx.showModal({
            title: '提交失败！',
            content: '',
          })
        }

      },
      fail: function() {
        wx.showModal({
          title: '提交失败！',
          content: '',
        })
      }
    })
    console.log(e, "表单数据", this.data.city)
  },
  bindRegionChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  goTo: function(e) {
    var id = e.currentTarget.dataset.id
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '../article/article?id=' + id,
    })
  },
  //幻灯片
  changeIndicatorDots: function(e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function(e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function(e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function(e) {
    this.setData({
      duration: e.detail.value
    })
  },

  //联动选择器
  bindChange: function(e) {
    console.log(e);
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.cityData;
    // --------
    this.setData({

    })
    // --------
    if (val[0] != t[0]) {
      console.log('province no ');
      const citys = [];
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i].name)
      }
      for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
        countys.push(cityData[val[0]].sub[0].sub[i].name)
      }

      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].sub[0].name,
        citys: citys,
        county: cityData[val[0]].sub[0].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], 0, 0]
      })

      return;
    }
    if (val[1] != t[1]) {
      console.log('city no');
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
      }

      this.setData({
        city: this.data.citys[val[1]],
        county: cityData[val[0]].sub[val[1]].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], val[1], 0]
      })
      return;
    }
    if (val[2] != t[2]) {
      console.log('county no');
      this.setData({
        county: this.data.countys[val[2]],
        values: val
      })
      return;
    }
  },
  open: function() {
    this.setData({
      condition: !this.data.condition
    })
  },
  showView: function() {
    this.setData({
      condition: true
    })
  },

  ccityInit: function() {
    var that = this
    tcity.init(this);

    var cityData = that.data.cityData;


    const provinces = [];
    const citys = [];
    const countys = [];

    for (let i = 0; i < cityData.length; i++) {
      provinces.push(cityData[i].name);
    }
    console.log('省份完成');
    for (let i = 0; i < cityData[0].sub.length; i++) {
      citys.push(cityData[0].sub[i].name)
    }
    console.log('city完成');
    for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
      countys.push(cityData[0].sub[0].sub[i].name)
    }

    that.setData({
      'provinces': provinces,
      'citys': citys,
      'countys': countys,
      'province': cityData[0].name,
      'city': cityData[0].sub[0].name,
      'county': cityData[0].sub[0].sub[0].name
    })
    console.log(provinces)
    console.log('初始化完成');
  },
  bindName: function(e) {
    var namevalue = e.detail.value
    console.log(namevalue)
    this.setData({
      namevalue: namevalue
    })
  },
  bindPhone: function(e) {
    var phonevalue = e.detail.value;
    console.log(phonevalue.length);
    this.setData({
      phonevalue: phonevalue
    })
    // var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    // if (this.data.phonevalue == "") {
    //   wx.showToast({
    //     title: '手机号不能为空',
    //     icon: 'none',
    //     duration: 1000
    //   })
    //   return;
    // }else if (phonevalue.length<11 && !myreg.test(this.data.phonevalue)) {
    //     wx.showToast({
    //       title: '请输入正确的手机号',
    //       icon: 'none',
    //       duration: 1000
    //     })
    //     return;
    // }
  },
  bindPlace: function(e) {
    var placevalue = e.detail.value
    this.setData({
      placevalue: placevalue
    })
    console.log(this.data.placevalue)
  },
  selAdree: function() {
    var that = this
    if (that.data.selSta == 1) {
      that.setData({
        selSta: 2
      })
    } else if (that.data.selSta == 2) {
      that.setData({
        selSta: 1
      })
    }
  },
  onLoad: function() {
    console.log("onLoad");
    var that = this;
    this.ccityInit()
  }
})